package com.fly.feishu.web.helper;

import com.fly.feishu.api.dto.WebAccessToken;
import com.fly.feishu.infrastructure.persistent.po.Employee;

/**
 * 飞书登陆用户身份信息
 */
public class FeishuUserContext {
    /**
     * 飞书登陆用户信息
     */
    private static ThreadLocal<WebAccessToken> webAccessTokenThreadLocal = new ThreadLocal<>();
    /**
     * 本地用户信息
     */
    private static ThreadLocal<Employee> webTokenThreadLocal = new ThreadLocal<>();

    public static WebAccessToken getCurrentWebToke() {
        if (webAccessTokenThreadLocal != null) {
            return webAccessTokenThreadLocal.get();
        }
        return null;
    }

    public static Employee getCurrentToken() {
        if (webTokenThreadLocal != null) {
            return webTokenThreadLocal.get();
        }
        return null;
    }

    public static void setWebAccessTokenThreadLocal(WebAccessToken webAccessToken) {
        if (webAccessTokenThreadLocal == null) {
            webAccessTokenThreadLocal = new ThreadLocal<>();
        }
        webAccessTokenThreadLocal.set(webAccessToken);
    }

    public static void setWebTokenThreadLocal(Employee employee) {
        if (webTokenThreadLocal == null) {
            webTokenThreadLocal = new ThreadLocal<>();
        }
        webTokenThreadLocal.set(employee);
    }

    public static void clear() {
        if (webAccessTokenThreadLocal != null && webAccessTokenThreadLocal.get() != null) {
            webAccessTokenThreadLocal.remove();
        }
        if (webTokenThreadLocal != null && webTokenThreadLocal.get() != null) {
            webTokenThreadLocal.remove();
        }
    }

}
